package com.openbravo.pos.config;

import com.openbravo.data.user.DirtyManager;
import com.openbravo.pos.forms.AppConfig;
import com.openbravo.pos.forms.AppLocal;

import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;

public class JPanelConfigLocale extends JPanel
  implements PanelConfig
{
  private DirtyManager dirty = new DirtyManager();
  private static final String DEFAULT_VALUE = "(Default)";
  private JLabel jLabel1;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JLabel jLabel4;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JLabel jLabel8;
  private JPanel jPanel1;
  private JComboBox jcboCurrency;
  private JComboBox jcboDate;
  private JComboBox jcboDatetime;
  private JComboBox jcboDouble;
  private JComboBox jcboInteger;
  private JComboBox jcboLocale;
  private JComboBox jcboPercent;
  private JComboBox jcboTime;

  public JPanelConfigLocale()
  {
    initComponents();
    this.jcboLocale.addActionListener(this.dirty);
    this.jcboInteger.addActionListener(this.dirty);
    this.jcboDouble.addActionListener(this.dirty);
    this.jcboCurrency.addActionListener(this.dirty);
    this.jcboPercent.addActionListener(this.dirty);
    this.jcboDate.addActionListener(this.dirty);
    this.jcboTime.addActionListener(this.dirty);
    this.jcboDatetime.addActionListener(this.dirty);
    ArrayList localArrayList = new ArrayList();
    localArrayList.addAll(Arrays.asList(Locale.getAvailableLocales()));
    addLocale(localArrayList, new Locale("eu", "ES", ""));
    addLocale(localArrayList, new Locale("gl", "ES", ""));
    Collections.sort(localArrayList, new LocaleComparator());
    this.jcboLocale.addItem(new LocaleInfo(null));
    Iterator localIterator = localArrayList.iterator();
    while (localIterator.hasNext())
    {
      Locale localLocale = (Locale)localIterator.next();
      this.jcboLocale.addItem(new LocaleInfo(localLocale));
    }
    this.jcboInteger.addItem("(Default)");
    this.jcboInteger.addItem("#0");
    this.jcboInteger.addItem("#,##0");
    this.jcboDouble.addItem("(Default)");
    this.jcboDouble.addItem("#0.0");
    this.jcboDouble.addItem("#,##0.#");
    this.jcboCurrency.addItem("(Default)");
    this.jcboCurrency.addItem("¤ #0.00");
    this.jcboCurrency.addItem("'$' #,##0.00");
    this.jcboPercent.addItem("(Default)");
    this.jcboPercent.addItem("#,##0.##%");
    this.jcboDate.addItem("(Default)");
    this.jcboTime.addItem("(Default)");
    this.jcboDatetime.addItem("(Default)");
  }

  private void addLocale(List<Locale> paramList, Locale paramLocale)
  {
    if (!paramList.contains(paramLocale))
      paramList.add(paramLocale);
  }

  public boolean hasChanged()
  {
    return this.dirty.isDirty();
  }

  public Component getConfigComponent()
  {
    return this;
  }

  public void loadProperties(AppConfig paramAppConfig)
  {
    String str1 = paramAppConfig.getProperty("user.language");
    String str2 = paramAppConfig.getProperty("user.country");
    String str3 = paramAppConfig.getProperty("user.variant");
    if ((str1 != null) && (!str1.equals("")) && (str2 != null) && (str3 != null))
    {
      Locale localLocale = new Locale(str1, str2, str3);
      for (int i = 0; i < this.jcboLocale.getItemCount(); i++)
      {
        LocaleInfo localLocaleInfo = (LocaleInfo)this.jcboLocale.getItemAt(i);
        if (!localLocale.equals(localLocaleInfo.getLocale()))
          continue;
        this.jcboLocale.setSelectedIndex(i);
        break;
      }
    }
    else
    {
      this.jcboLocale.setSelectedIndex(0);
    }
    this.jcboInteger.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.integer")));
    this.jcboDouble.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.double")));
    this.jcboCurrency.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.currency")));
    this.jcboPercent.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.percent")));
    this.jcboDate.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.date")));
    this.jcboTime.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.time")));
    this.jcboDatetime.setSelectedItem(writeWithDefault(paramAppConfig.getProperty("format.datetime")));
    this.dirty.setDirty(false);
  }

  public void saveProperties(AppConfig paramAppConfig)
  {
    Locale localLocale = ((LocaleInfo)this.jcboLocale.getSelectedItem()).getLocale();
    if (localLocale == null)
    {
      paramAppConfig.setProperty("user.language", "");
      paramAppConfig.setProperty("user.country", "");
      paramAppConfig.setProperty("user.variant", "");
    }
    else
    {
      paramAppConfig.setProperty("user.language", localLocale.getLanguage());
      paramAppConfig.setProperty("user.country", localLocale.getCountry());
      paramAppConfig.setProperty("user.variant", localLocale.getVariant());
    }
    paramAppConfig.setProperty("format.integer", readWithDefault(this.jcboInteger.getSelectedItem()));
    paramAppConfig.setProperty("format.double", readWithDefault(this.jcboDouble.getSelectedItem()));
    paramAppConfig.setProperty("format.currency", readWithDefault(this.jcboCurrency.getSelectedItem()));
    paramAppConfig.setProperty("format.percent", readWithDefault(this.jcboPercent.getSelectedItem()));
    paramAppConfig.setProperty("format.date", readWithDefault(this.jcboDate.getSelectedItem()));
    paramAppConfig.setProperty("format.time", readWithDefault(this.jcboTime.getSelectedItem()));
    paramAppConfig.setProperty("format.datetime", readWithDefault(this.jcboDatetime.getSelectedItem()));
    this.dirty.setDirty(false);
  }

  private String readWithDefault(Object paramObject)
  {
    if ("(Default)".equals(paramObject))
      return "";
    return paramObject.toString();
  }

  private Object writeWithDefault(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")) || (paramString.equals("(Default)")))
      return "(Default)";
    return paramString.toString();
  }

  private void initComponents()
  {
    this.jPanel1 = new JPanel();
    this.jLabel5 = new JLabel();
    this.jcboLocale = new JComboBox();
    this.jLabel1 = new JLabel();
    this.jcboInteger = new JComboBox();
    this.jLabel2 = new JLabel();
    this.jcboDouble = new JComboBox();
    this.jLabel3 = new JLabel();
    this.jcboCurrency = new JComboBox();
    this.jLabel4 = new JLabel();
    this.jcboPercent = new JComboBox();
    this.jLabel6 = new JLabel();
    this.jcboDate = new JComboBox();
    this.jLabel7 = new JLabel();
    this.jcboTime = new JComboBox();
    this.jLabel8 = new JLabel();
    this.jcboDatetime = new JComboBox();
    this.jPanel1.setBorder(BorderFactory.createTitledBorder(AppLocal.getIntString("label.locale")));
    this.jLabel5.setText(AppLocal.getIntString("label.locale"));
    this.jLabel1.setText(AppLocal.getIntString("label.integer"));
    this.jcboInteger.setEditable(true);
    this.jLabel2.setText(AppLocal.getIntString("label.double"));
    this.jcboDouble.setEditable(true);
    this.jLabel3.setText(AppLocal.getIntString("label.currency"));
    this.jcboCurrency.setEditable(true);
    this.jLabel4.setText(AppLocal.getIntString("label.percent"));
    this.jcboPercent.setEditable(true);
    this.jLabel6.setText(AppLocal.getIntString("label.date"));
    this.jcboDate.setEditable(true);
    this.jLabel7.setText(AppLocal.getIntString("label.time"));
    this.jcboTime.setEditable(true);
    this.jLabel8.setText(AppLocal.getIntString("label.datetime"));
    this.jcboDatetime.setEditable(true);
    GroupLayout localGroupLayout1 = new GroupLayout(this.jPanel1);
    this.jPanel1.setLayout(localGroupLayout1);
    localGroupLayout1.setHorizontalGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout1.createSequentialGroup().addContainerGap().addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel5, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboLocale, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel1, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboInteger, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel2, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboDouble, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel3, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboCurrency, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel4, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboPercent, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel6, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboDate, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel7, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboTime, -2, 165, -2)).addGroup(localGroupLayout1.createSequentialGroup().addComponent(this.jLabel8, -2, 130, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.jcboDatetime, -2, 165, -2))).addContainerGap(265, 32767)));
    localGroupLayout1.setVerticalGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout1.createSequentialGroup().addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jcboLocale, -2, -1, -2).addComponent(this.jLabel5)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel1).addComponent(this.jcboInteger, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel2).addComponent(this.jcboDouble, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel3).addComponent(this.jcboCurrency, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel4).addComponent(this.jcboPercent, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel6).addComponent(this.jcboDate, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel7).addComponent(this.jcboTime, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout1.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel8).addComponent(this.jcboDatetime, -2, -1, -2)).addContainerGap(-1, 32767)));
    GroupLayout localGroupLayout2 = new GroupLayout(this);
    setLayout(localGroupLayout2);
    localGroupLayout2.setHorizontalGroup(localGroupLayout2.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout2.createSequentialGroup().addContainerGap().addComponent(this.jPanel1, -1, -1, 32767).addContainerGap()));
    localGroupLayout2.setVerticalGroup(localGroupLayout2.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout2.createSequentialGroup().addContainerGap().addComponent(this.jPanel1, -2, -1, -2).addContainerGap(-1, 32767)));
  }

  private static class LocaleInfo
  {
    private Locale locale;

    public LocaleInfo(Locale paramLocale)
    {
      this.locale = paramLocale;
    }

    public Locale getLocale()
    {
      return this.locale;
    }

    public String toString()
    {
      return this.locale == null ? "(System default)" : this.locale.getDisplayName();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.config.JPanelConfigLocale
 * JD-Core Version:    0.6.0
 */